package com.ssy.invoicing.customer.controller;

import java.sql.SQLException;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ssy.invoicing.common.controller.BaseController;
import com.ssy.invoicing.customer.bean.CustomerBean;
import com.ssy.invoicing.customer.service.CustomerService;
import com.ssy.invoicing.dao.support.BaseSqlDao.SplitPageResult;

/**
 * 客户管理Controller
 * Comment of CustomerController
 * @author suoyao.shen
 */
@Controller
@RequestMapping("invoicing")
public class CustomerController extends BaseController {
    
    Logger logger = LoggerFactory.getLogger(CustomerController.class);
    
    @Resource(name="customerService")
    private CustomerService customerService;

    @RequestMapping("addCustomer")
    public ModelAndView addOrder() {
        return new ModelAndView("pages/customer/add");
    }
    
    /**
     * 查询客户信息
     * @author suoyao.shen
     * @param model
     * @return
     */
    @RequestMapping("listCustomer")
    public ModelAndView listCustomer(Integer page, ModelMap model) {
        try {
            SplitPageResult spr = customerService.queryCustomerList(page);
            model.put("pagination", spr.getPagination());
            model.put("customerList", spr == null ? null : spr.getResult());
        } catch (SQLException e) {
            logger.error("query customer list fail.",e);
            return new ModelAndView("");
        }
        return new ModelAndView("pages/customer/list");
    }
    
    /**
     * 获得用户详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("editCustomer")
    public ModelAndView editCustomer(Integer id,ModelMap model) {
        CustomerBean customer = customerService.getCustomer(id);
        model.put("customer", customer);
        return new ModelAndView("pages/customer/edit");
    }
    
    /**
     * 删除用户信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("removeCustomer")
    public ModelAndView removeCustomer(Integer id,ModelMap model) {
        customerService.removeCustomer(id);
        return new ModelAndView("redirect:listCustomer.htm");
    }
    
    /**
     * 保存客户信息
     * @author suoyao.shen
     * @param customer
     * @param model
     * @return
     */
    @RequestMapping("saveCustomer")
    public ModelAndView saveCustomer(CustomerBean customer, ModelMap model) {
        customerService.saveCustomer(customer);
        return new ModelAndView("redirect:listCustomer.htm");
    }
    
    /**
     * 更新客户信息
     * @param customer
     * @param model
     * @return
     */
    @RequestMapping("updateCustomer")
    public ModelAndView updateCustomer(CustomerBean customer, ModelMap model) {
        customer.setAddTime(new Date());
        customerService.updateCustomer(customer);
        return new ModelAndView("redirect:listCustomer.htm");
    }
    
}
